import axios from "axios";
import type { AxiosRequestConfig, AxiosResponse }  from "axios";

class Axios {
  private baseURL: string;

  constructor(baseURL: string) {
    this.baseURL = baseURL;
  }

  // 发起 GET 请求
  private get<T>(
    url: string,
    config?: AxiosRequestConfig
  ): Promise<AxiosResponse<T>> {
    return axios.get<T>(this.baseURL + url, config);
  }

  // 发起 POST 请求
  private post<T>(
    url: string,
    data?: any,
    config?: AxiosRequestConfig
  ): Promise<AxiosResponse<T>> {
    return axios.post<T>(this.baseURL + url, data, config);
  }

  // 用户登陆API
  public loginUser(
    email: string,
    password: string
  ): Promise<AxiosResponse<any>> {
    const url = "/Account/Login";
    const params = {
      email,
      password,
    };
    return this.get(url, { params });
  }

  // 用户注册API
  public registerUser(
    email: string,
    password: string
  ): Promise<AxiosResponse<any>> {
    const url = "/Account/RegisterAccount";
    const data = {
      email,
      password,
    };
    return this.post(url, data);
  }

  // 激活用户API
  public verifyUser(
    email: string,
    password: string,
    emailAuthCode: string
  ): Promise<AxiosResponse<any>> {
    const url = "/Account/VerifyUser";
    const data = {
      email,
      password,
      emailAuthCode,
    };
    return this.post(url, data);
  }
}


const baseURL = "http://123.249.127.238:8080";
/**
 * # Axios单例
 * 
 * ## 用户登陆 API 调用示例
 * ```ts
 * requests
 *   .loginUser("291148484@163.com", "123456")
 *   .then((response) => {
 *     console.log(response.status);
 *     console.log(response.data);
 *   })
 *   .catch((error) => {
 *     console.error(error);
 *   });
 * ```
 * 
 * ## 用户注册 API 调用示例
 * ```ts
 * requests
 *   .registerUser("291148484@qq.com", "123456")
 *   .then((response) => {
 *     console.log(response.status);
 *     console.log(response.data);
 *   })
 *   .catch((error) => {
 *     console.error(error);
 *   });
 * ```
 * ## 激活用户 API 调用示例
 * ```ts
 * requests
 *   .verifyUser("xxx@xx.com", "123456", "cd3f9c9d-3735-4090-88d4-29f6a05a65ff")
 *   .then((response) => {
 *     console.log(response.status);
 *     console.log(response.data);
 *   })
 *   .catch((error) => {
 *     console.error(error);
 *   });
 * ```
 */
const requests = new Axios(baseURL);

export {
  requests,
}


